package com.muyi.controller.comment;

import com.alibaba.fastjson.JSON;
import com.muyi.pojo.entity.Comment;
import com.muyi.pojo.vo.Result;
import com.muyi.service.CommentService;
import com.muyi.service.impl.CommentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "ServletUpdateComment", value = "/updateComment")
public class ServletUpdateComment extends HttpServlet {
    private CommentService commentService = new CommentServiceImpl();
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取请求参数
        //获取请求参数
        int comment_id = Integer.parseInt(request.getParameter("comment_id"));
        int paper_id = Integer.parseInt(request.getParameter("paper_id"));
        int user_id = Integer.parseInt(request.getParameter("user_id"));
        int like_count = Integer.parseInt(request.getParameter("like_count"));
        Date publish_date = null;
        try {
            publish_date = sdf.parse(request.getParameter("publish_date"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int favorite_count = Integer.parseInt(request.getParameter("favorite_count"));
        String content = request.getParameter("content");
        //调用service层方法
        Comment comment = new Comment(comment_id, paper_id, user_id, publish_date, like_count, favorite_count, content);
        Result result = null;
        try {
            result = commentService.updateComment(comment);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //将执行结果返回给前端
        response.getWriter().write(JSON.toJSONString(result));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
